﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;

public partial class adm_addNewSubCate : System.Web.UI.Page
{
    CodeDatDataContext _db = new CodeDatDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.loadCate();
            var itemCate = from ct in _db.Categories
                           where ct.Display == true
                           select ct;
            DropDownList1.DataSource = itemCate;
            DropDownList1.DataTextField = "CateTitle";
            DropDownList1.DataValueField = "CateID";
            DropDownList1.DataBind();
        }
    }

    void loadCate()
    {
        var itemCate = from ct in _db.Categories
                       select ct;
        dropCate.DataSource = itemCate;
        dropCate.DataTextField = "CateTitle";
        dropCate.DataValueField = "CateID";
        dropCate.DataBind();
    }
    void loadVB(int subCateid)
    {
        var itemVB = from vb in _db.DTVBs
                     where vb.SubCateID==subCateid
                     select vb ;
        GridView2.DataSource=itemVB;
        GridView2.DataBind();
    }
     void loadSubCate(int idCate)
    {
        var itemSCate = from sct in _db.SubCates
                        where sct.CateID==idCate
                        select sct;
        GridView1.DataSource = itemSCate;
        GridView1.DataBind();
    }
     void loadSubCate2()
     {
         var itemSCate54 = from sct in _db.SubCates
                         select sct;
         GridView1.DataSource = itemSCate54;
         GridView1.DataBind();
     }
    protected void dropCate_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idCateDr = int.Parse(dropCate.SelectedValue.ToString());
        loadSubCate(idCateDr);
        //UpdatePanel2.Update();
        //GridView2.DataSource = null;
        //GridView2.DataBind();
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {       
        try
        {
            int idVB = int.Parse(GridView1.SelectedRow.Cells[0].Text);
            if (idVB != 0)
            {
                 loadVB(idVB);
                 SubCate sub = _db.SubCates.Single(ida=>ida.SubCateID==idVB);

                 txtTenVB.Text = sub.SubCateTitle.ToString();
                 ckLVB.Checked = (bool)sub.Display;
                 hidIDLoaiVB.Value = sub.SubCateID.ToString();
                var itemCate2 = from ct in _db.Categories
                       where ct.Display == true 
                       select ct;
                dropLoaiVBEdit.DataSource = itemCate2;
                dropLoaiVBEdit.DataTextField = "CateTitle";
                dropLoaiVBEdit.DataValueField = "CateID";
                dropLoaiVBEdit.DataBind();
                dropLoaiVBEdit.SelectedIndex = (int)sub.CateID-1;
                // Xoa table App
                this.tableclearThemApp();
            }
            else
            {
               // GridView2.DataSource = null;
               // GridView2.DataBind();
            }
        }
        catch
        {

        }

    }
   
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        int SubCode = int.Parse(hidIDLoaiVB.Value.ToString());
        SubCate sucb = _db.SubCates.Single(iamc=>iamc.SubCateID==SubCode);
        
        sucb.CateID=int.Parse(dropLoaiVBEdit.SelectedValue.ToString());
        sucb.SubCateTitle = txtTenVB.Text.Trim();
        sucb.Display = ckLVB.Checked;

        _db.SubmitChanges();

        loadSubCate(int.Parse(dropLoaiVBEdit.SelectedValue.ToString()));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SubCate sbcca = new SubCate()
        {
            CateID=int.Parse(dropLoaiVBEdit.SelectedValue.ToString()),
            Display=ckLVB.Checked,
            SubCateTitle=txtTenVB.Text.Trim()
        };
        _db.SubCates.InsertOnSubmit(sbcca);
        _db.SubmitChanges();
        loadSubCate(int.Parse(dropLoaiVBEdit.SelectedValue.ToString()));
    }

    protected void btn_xoaluon_Click(object sender, EventArgs e)
    {
        int subCode2 = int.Parse(hidIDLoaiVB.Value.ToString());
        SubCate subc2 = _db.SubCates.Single(sucaid=>sucaid.SubCateID==subCode2);
        _db.SubCates.DeleteOnSubmit(subc2);
        _db.SubmitChanges();
        loadSubCate(int.Parse(dropLoaiVBEdit.SelectedValue.ToString()));

    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int idApp = int.Parse(GridView2.SelectedRow.Cells[0].Text);
        DTVB appsubc = _db.DTVBs.Single(apcdda => apcdda.AppID == idApp);

        txtTenDon.Text = appsubc.AppTitle.ToString();
        txtMoTaVB.Text = appsubc.TitleDescript.ToString();
        txtGia.Text = appsubc.AppPrice.ToString();
        CheckBox1.Checked = (bool)appsubc.Display;
        HiddenField2.Value = appsubc.AppID.ToString();

        var itemCate4 = from ct3 in _db.SubCates
                        where ct3.Display == true
                        select ct3;
        DropDownList1.DataSource = itemCate4;
        DropDownList1.DataTextField = "SubCateTitle";
        DropDownList1.DataValueField = "SubCateID";
        DropDownList1.DataBind();
        //DropDownList1.SelectedIndex = (int)appsubc.SubCateID;
        DropDownList1.SelectedValue = appsubc.SubCateID.ToString();
       // Response.Write(appsubc.SubCateID.ToString());

        var itemtp = from tp in _db.Cities
                     where tp.Display == true
                     select tp;
        DropDownList2.DataSource = itemtp;
        DropDownList2.DataTextField = "CityName";
        DropDownList2.DataValueField = "CityID";
        DropDownList2.DataBind();
        DropDownList2.SelectedValue = appsubc.CityID.ToString();
        //Response.Write(appsubc.CityID.ToString());
    }
    void tableclearThemApp()
    {
        txtTenDon.Text = "";
        txtMoTaVB.Text = "";
        txtGia.Text = "";
        CheckBox1.Checked = false;
    }

    protected void btnSuaApp_Click(object sender, EventArgs e)
    {
        int maApp = int.Parse(HiddenField2.Value.ToString());
        DTVB app34 = _db.DTVBs.Single(apclf=>apclf.AppID==maApp);
        app34.AppTitle = txtTenDon.Text.Trim();
        app34.AppPrice = Convert.ToDouble((txtGia.Text.Trim()));
        app34.Display = CheckBox1.Checked;
        app34.TitleDescript = txtMoTaVB.Text.Trim();

        _db.SubmitChanges();
        this.loadVB(int.Parse(DropDownList1.SelectedValue.ToString()));
    }
    protected void btnThemApp_Click(object sender, EventArgs e)
    {
        DTVB dtvb = new DTVB()
        {
            SubCateID = int.Parse(DropDownList1.SelectedValue.ToString()),
            Display = ckLVB.Checked,
            TitleDescript = txtMoTaVB.Text.Trim(),
            AppTitle=txtTenDon.Text.Trim(),
            AppPrice=Convert.ToDouble(txtGia.Text.Trim()),
            CityID=int.Parse(DropDownList2.SelectedValue.ToString()),
        };
        _db.DTVBs.InsertOnSubmit(dtvb);
        _db.SubmitChanges();

        this.loadVB(int.Parse(DropDownList1.SelectedValue.ToString()));
        this.tableclearThemApp();
    }
}
